import Ember from 'ember';
import FormState from 'emberworkspace/utils/form-state';

export default Ember.Controller.extend({
  pageTitle: "用户管理",
  pageSubTitle: "编辑用户",
  curfn: "",
  formState: FormState.create({btnName: "保存", successMsg: "成功", failMsg: "失败"}),
  actions: {
    submitForm() {
      var model = this.get('model'),
          store = this.store,
          formState = this.get('formState'),
          self = this,
          rr = model.validateMe();

      // console.log(model.toJSON());
      if (!formState.allowSubmit()) {
        return;
      }
      if (rr) {
        formState.setError(rr);
      } else {
        formState.startSaving();
        model.save().then(function(){
          formState.success();
        }, function(){
          if (formState.is201(model.get('adapterError'))) {
            self.transitionTo("quotas");
          } else {
            formState.failure();
          }
        });
      }
    },
    nameFocused(v) {
      this.set('curfn', "用户名");
    },
    emailFocused(v) {
      this.set('curfn', "Email");
    },
    mobileFocused(v) {
      this.set('curfn', "手机号码");
    },
    passwordFocused(v) {
      this.set('curfn', "密码");
    },
    repeatPasswordFocused(v) {
      this.set('curfn', "重复密码");
    },
  }
});
